package ftp

import (
	"fmt"
	"gin/model"
	"strconv"
	"strings"
	"time"
)

// 开锁日志
func (*DataParsing) parsingLockLog(catData []string, ftpInfoId int64) []model.DataLock {
	var logDate, LogTime, steamId, gameName, gameId, lockEvent, dataType string
	var lockSteamId, lockGameName, lockgGameId, lockStatus, lockTime, lockNum, lockObj, lockType, lockLocation string
	var end int
	var dataArr []model.DataLock
	//允许的解析的事件列表
	lockEventList := map[string]string{
		"LockpickingMinigame_C": "一般锁",
		"BP_DialLockMinigame_C": "一般锁",
	}
	for _, logTxt := range catData {
		//fmt.Println("原始数据", logTxt)

		parts := strings.Split(logTxt, ":")
		//fmt.Println("分割数据", parts)
		timeParts := strings.Split(parts[0], "-")
		//fmt.Println("分割数据2", timeParts)
		//日期转换  年-月-日
		logDate = strings.Replace(timeParts[0], ".", "-", -1)
		//fmt.Println("转换数据1", catDate)
		//时间转换  时:分:秒
		Time := strings.Replace(timeParts[1], ".", ":", -1)
		//fmt.Println("转换数据2", catTime)
		layout := "2006-01-02 15:04:05"
		// 解析时间字符串
		//fmt.Println("解析时间格式为", catDate+" "+catTime)
		t, err := time.Parse(layout, logDate+" "+Time)
		if err != nil {
			fmt.Println("解析时间格式为", err.Error())
			LogTime = "0"
			//return
		}
		// 转换为 Unix 时间戳（秒）

		LogTime = strconv.FormatInt(t.Unix(), 10)
		logTime, _ := strconv.ParseInt(LogTime, 10, 64)
		//太平洋时间制  + 8 小时
		//logTime = logTime + 3600*8
		t = time.Unix(logTime, 0)
		logDate = t.Format("2006-01-02")

		hour, minute, second := t.Clock()
		Time = fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)

		//开锁类型过滤
		start := strings.Index(logTxt, ": [")
		if start < 0 {
			dataType = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+3:], "]")
			if end < 0 {
				dataType = ""
			} else {
				dataType = logTxt[start+3 : start+3+end]
			}
		}

		if dataType != "LogMinigame" {
			//不是开锁数据跳过
			continue
		}

		//开锁触发事件
		start = strings.Index(logTxt, "] [")
		if start < 0 {
			lockEvent = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+3:], "]")
			if end < 0 {
				lockEvent = ""
			} else {
				lockEvent = logTxt[start+3 : start+3+end]
			}
		}

		if lockEventList[lockEvent] == "" {
			//不是指定的事件类型  暂时跳过 防止解析出错  ，其他事件类型 以后解析
			continue
		}

		//获取steamId
		start = strings.Index(logTxt, ", ")
		//fmt.Println("位置1", start)
		if start < 0 {
			steamId = ""
		} else {
			end = strings.Index(logTxt[start+2:], ")")
			if end < 0 {
				steamId = ""
			} else {
				steamId = logTxt[start+2 : start+2+end]
			}
		}

		//fmt.Println("steamid", steamId)

		//游戏名名称获取
		start = strings.Index(logTxt, "User: ")
		//fmt.Println("位置1", start)
		if start < 0 {
			gameName = ""
		} else {
			end = strings.Index(logTxt[+start+6:], " (")
			if end < 0 {
				gameName = ""
			} else {
				gameName = logTxt[+start+6 : start+6+end]
			}
		}

		//游戏名id获取
		start = strings.Index(logTxt, "(")
		if start < 0 {
			gameId = ""
			//fmt.Println("找不到单引号")
			//return
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+1:], ",")
			if end < 0 {
				gameId = ""
				//fmt.Println("找不到第二个单引号")
				//return
			} else {
				// 提取引号内的数据
				gameId = logTxt[start+1 : start+1+end]
			}
		}

		// 开锁状态
		start = strings.Index(logTxt, "ss: ")
		if start < 0 {
			lockStatus = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+4:], ".")
			if end < 0 {
				lockStatus = ""
			} else {
				lockStatus = logTxt[start+4 : start+4+end]
			}
		}

		// 开锁时间
		start = strings.Index(logTxt, "time: ")
		if start < 0 {
			lockTime = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+6:], ".")
			if end < 0 {
				lockTime = ""
			} else {
				lockTime = logTxt[start+6 : start+6+end]
			}
		}

		// 开锁次数
		start = strings.Index(logTxt, "pts: ")
		if start < 0 {
			lockNum = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+5:], ".")
			if end < 0 {
				lockNum = ""
			} else {
				lockNum = logTxt[start+5 : start+5+end]
			}
		}

		// 开锁对象
		start = strings.Index(logTxt, "ect: ")
		if start < 0 {
			lockObj = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+5:], ".")
			if end < 0 {
				lockObj = ""
			} else {
				lockObj = logTxt[start+5 : start+5+end]
			}
		}

		// 锁类型
		start = strings.Index(logTxt, "type: ")
		if start < 0 {
			lockType = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+6:], ".")
			if end < 0 {
				lockType = ""
			} else {
				lockType = logTxt[start+6 : start+6+end]
			}
		}

		// 锁类型
		start = strings.Index(logTxt, "owner: ")
		if start < 0 {
			lockgGameId = ""
			lockSteamId = ""
			lockGameName = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+7:], ".")
			if end < 0 {
				lockgGameId = ""
				lockSteamId = ""
				lockGameName = ""
			} else {
				data := logTxt[start+7 : start+7+end]
				//NA 代表 锁不是用户的
				if data == "N/A" {
					lockgGameId = ""
					lockSteamId = ""
					lockGameName = ""
				} else {
					//用户锁

					//锁用户id
					start = strings.Index(data, "(")
					lockgGameId = data[:start]

					//锁 steamid
					start = strings.Index(data, "([")
					if start < 0 {
						lockSteamId = ""
					} else {
						// 查找第二个单引号的位置
						end = strings.Index(data[start+2:], "]")
						if end < 0 {
							lockSteamId = ""
						} else {
							lockSteamId = data[start+2 : start+2+end]
						}
					}

					//锁 锁用户名
					start = strings.Index(data, "] ")
					if start < 0 {
						lockGameName = ""
					} else {
						// 查找第二个单引号的位置
						end = strings.Index(data[start+2:], ")")
						if end < 0 {
							lockGameName = ""
						} else {
							lockGameName = data[start+2 : start+2+end]
							lockGameName = TruncateStringByCharCount(lockGameName, 50)

							// 输出字节集（以十六进制表示）
							//fmt.Printf("Byte slice: %x\n", byteSlice)

							// 将字节集转换回字符串
							lockGameName = filterInvalidChars(lockGameName)
						}
					}

				}

			}
		}

		start = strings.Index(logTxt, "ion: ")

		if start < 0 {
			lockLocation = ""
		} else {
			// 查找第二个单引号的位置
			lockLocation = logTxt[start+5:]
		}

		//fmt.Println("数据为：", ftpInfoId, logDate, Time, LogTime, steamId, gameName, gameId, loginType, loginIp, location)

		gameId, _ := strconv.ParseInt(gameId, 10, 64)
		lockgGameId, _ := strconv.ParseInt(lockgGameId, 10, 64)
		lockNum, _ := strconv.ParseInt(lockNum, 10, 64)
		downloadFile := model.DataLock{FtpInfoId: ftpInfoId, Date: logDate, Time: Time, LogTime: logTime, SteamId: steamId, GameName: gameName, GameId: gameId, LockStatus: lockStatus, LockTime: lockTime, LockNum: lockNum, LockObj: lockObj, LockType: lockType, LockGameId: lockgGameId, LockSteamId: lockSteamId, LockGameName: lockGameName, LockLocation: lockLocation, LockEvent: lockEvent}
		dataArr = append(dataArr, downloadFile)
	}
	return dataArr
}
